<template>
	<view class="container container329916">
		<u-form-item :borderBottom="false" v-if="globalData.isshow" class="diygw-col-24 title-clz" labelPosition="top" prop="title">
			<u-input @blur="listApi" :data-title_like="title" :focus="titleFocus" placeholder="请输入产品标题" v-model="title"></u-input>
			<text @tap="navigateTo" data-type="listApi" :data-title_like="title" class="diy-icon-search" style="color: #363636; font-size: 32rpx"></text>
		</u-form-item>
		<view v-if="globalData.isshow" class="flex tabs diygw-col-24 flex-direction-row tabs-clz">
			<scroll-view scroll-y :scroll-top="tabsTop" scroll-with-animation class="diygw-tabs text-center solid-right justify-start small-border tabs-title" :style="{ height: tabsMainHeight }">
				<view class="diygw-tab-item tabs-item-title" :class="index == tabsIndex ? ' font-bold  cur text-red ' : ''" v-for="(item, index) in cates.rows" :key="index" @click="changeTabs" :data-index="index">
					{{ item.title }}
				</view>
			</scroll-view>
			<scroll-view scroll-y scroll-with-animation :scroll-into-view="'tabs-content-' + tabsMainIndex" @scroll="rightScrollTabs" class="flex1 tabs-content" :style="{ height: tabsMainHeight }">
				<view class="tabs-content-inner tabs-content flex-sub">
					<view class="flex flex-wrap diygw-col-24 items-center flex14-clz">
						<text class="diygw-col-0 text7-clz">
							{{ cates.rows[tabsIndex].title }}
						</text>
					</view>
					<view v-for="(item, index) in list.rows" :key="index" class="flex flex-wrap diygw-col-24 items-stretch flex2-clz" @tap="navigateTo" data-type="page" data-url="/pages/goods/detail" :data-id="item.id">
						<view class="flex flex-wrap diygw-col-0 items-center flex4-clz">
							<image :src="item.img" class="image1-size diygw-image diygw-col-0" mode="aspectFit"></image>
						</view>
						<view class="flex flex-wrap diygw-col-0 flex-direction-column justify-between flex20-clz">
							<text class="diygw-col-0 text11-clz">
								{{ item.title }}
							</text>
							<text class="diygw-text-line3 diygw-col-0">
								{{ item.remark }}
							</text>
							<view class="flex flex-wrap diygw-col-0 items-center">
								<text class="diygw-col-0 text8-clz"> ￥{{ item.price }} </text>
								<text class="diygw-text-line2 diygw-col-0 text9-clz"> ￥{{ item.linePrice }} </text>
							</view>
						</view>
					</view>
					<view v-if="globalData.isshow && list.code == 200 && list.total == 0" class="flex flex-wrap diygw-col-24 flex-direction-column items-center flex-clz">
						<image src="/static/zwjl.png" class="image-size diygw-image diygw-col-0" mode="widthFix"></image>
						<text class="diygw-col-0 text-clz"> 未找到任何数据 </text>
					</view>
					<view class="clearfix"></view>
				</view>
			</scroll-view>
		</view>
		<view id="bottom" class="flex flex-wrap diygw-col-24 items-end diygw-bottom flex1-clz">
			<view class="flex flex-wrap diygw-col-6 flex-direction-column items-center flex8-clz" @tap="navigateTo" data-type="page" data-url="/pages/index" data-redirect="1">
				<view class="flex flex-wrap diygw-col-0 flex-direction-column items-center">
					<image src="/static/sy3.png" class="image2-size diygw-image diygw-col-0" mode="widthFix"></image>
				</view>
				<text class="diygw-text-line1 diygw-col-0"> 首页 </text>
			</view>
			<view class="flex flex-wrap diygw-col-6 flex-direction-column items-center flex11-clz">
				<view class="flex flex-wrap diygw-col-0 flex-direction-column items-center">
					<image src="/static/flon.png" class="image8-size diygw-image diygw-col-0" mode="widthFix"></image>
				</view>
				<text class="diygw-text-line1 diygw-col-0"> 分类 </text>
			</view>
			<view class="flex flex-wrap diygw-col-6 flex-direction-column items-center flex5-clz" @tap="navigateTo" data-type="page" data-url="/pages/cart" data-redirect="1">
				<view class="flex flex-wrap diygw-col-0 flex-direction-column items-center">
					<text v-if="userInfo.carts && userInfo.carts.length > 0" class="diygw-text-line1 diygw-col-0 animate__animated animate__heartBeat animate__infinite text2-clz"> </text>
					<image src="/static/gwc.png" class="image5-size diygw-image diygw-col-0" mode="widthFix"></image>
				</view>
				<text class="diygw-text-line1 diygw-col-0"> 购物车 </text>
			</view>
			<view class="flex flex-wrap diygw-col-6 flex-direction-column items-center flex6-clz" @tap="navigateTo" data-type="page" data-url="/pages/articles" data-redirect="1">
				<view class="flex flex-wrap diygw-col-0 flex-direction-column items-center">
					<image src="/static/cp1.png" class="image3-size diygw-image diygw-col-0" mode="widthFix"></image>
				</view>
				<text class="diygw-text-line1 diygw-col-0"> 文章 </text>
			</view>
			<view class="flex flex-wrap diygw-col-6 flex-direction-column items-center flex12-clz" @tap="navigateTo" data-type="page" data-url="/pages/user" data-redirect="1">
				<view class="flex flex-wrap diygw-col-0 flex-direction-column items-center">
					<image src="/static/wd.png" class="image4-size diygw-image diygw-col-0" mode="widthFix"></image>
				</view>
				<text class="diygw-text-line1 diygw-col-0"> 我的 </text>
			</view>
		</view>
		<view class="clearfix"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//用户全局信息
				userInfo: {},
				//页面传参
				globalOption: {},
				//自定义全局变量
				globalData: { isshow: false },
				listNum: 1,
				list: {
					rows: [
						{
							id: 0,
							title: '',
							remark: '',
							img: '',
							imgs: '',
							status: '',
							content: '',
							price: '',
							amount: 0,
							sellamonut: null,
							skus: '',
							sortnum: null,
							cateId: 0,
							userId: 0,
							createTime: '',
							updateTime: '',
							deleteTime: null,
							linePrice: '',
							skuType: ''
						}
					],
					total: 0,
					code: 0,
					msg: ''
				},
				catesNum: 1,
				cates: {
					rows: [
						{
							id: 0,
							title: '',
							remark: null,
							img: '',
							sortnum: null,
							status: '',
							userId: 0,
							createTime: '',
							updateTime: '',
							deleteTime: null
						}
					],
					total: 0,
					code: 0,
					msg: ''
				},
				titleFocus: false,
				title: '',
				tabsMainHeight: '80vh',
				tabsTimer: null,
				tabsMainIndex: 0,
				tabsTop: 0,
				tabsHeight: 0,
				tabsItemHeight: 0,
				tabsLoad: true,
				tabsIndex: 0
			};
		},
		onShow() {
			this.setCurrentPage(this);
		},
		onLoad(option) {
			this.setCurrentPage(this);
			if (option) {
				this.setData({
					globalOption: this.getOption(option)
				});
			}

			this.init();
		},
		methods: {
			async init() {
				await this.listApi();
				await this.catesApi();
				await this.initTabsHeight();
			},
			// 列表数据 API请求方法
			async listApi(param) {
				let thiz = this;
				param = param || {};

				//如果请求要重置页面，请配置点击附加参数refresh=1  增加判断如输入框回调param不是对象
				if (param.refresh || typeof param != 'object') {
					this.listNum = 1;
				}

				//请求地址及请求数据，可以在加载前执行上面增加自己的代码逻辑
				let http_url = '/shop/api.goods/list';
				let http_data = {
					pageNum: this.listNum,
					pageSize: 10,
					title_like: param.title_like || this.title
				};
				let http_header = {};

				if (this.cates.rows.length > 0 && this.tabsIndex > 0) {
					let row = this.cates.rows[this.tabsIndex];
					if (row.id) {
						http_data.cateId = row.id;
					}
				}

				let list = await this.$http.post(http_url, http_data, http_header, 'json');

				let datarows = list.rows;
				if (http_data.pageNum == 1) {
					this.list = list;
				} else if (datarows) {
					let rows = this.list.rows.concat(datarows);
					list.rows = rows;
					this.list = list;
				}
				if (datarows && datarows.length > 0) {
					this.listNum = this.listNum + 1;
				}
				this.globalData.isshow = true;
			},
			// 分类数据 API请求方法
			async catesApi(param) {
				let thiz = this;
				param = param || {};

				//请求地址及请求数据，可以在加载前执行上面增加自己的代码逻辑
				let http_url = '/shop/api.cate/list';
				let http_data = {
					pageNum: this.catesNum,
					pageSize: 10,
					pageSize: param.pageSize || '100'
				};
				let http_header = {};

				let cates = await this.$http.post(http_url, http_data, http_header, 'json');

				cates.rows.unshift({ id: '', title: '全部' });

				this.cates = cates;
			},
			getTabsRect(selector) {
				let thiz = this;
				return new Promise((resolve) => {
					const query = uni.createSelectorQuery().in(thiz);
					query
						.select(selector)
						.boundingClientRect((rect) => {
							resolve(rect);
						})
						.exec();
				});
			},
			initTabsHeight() {
				let thiz = this;
				setTimeout(async () => {
					const systemInfo = uni.getSystemInfoSync();
					const rect = await this.getTabsRect('.tabs');
					if (!rect) {
						thiz.initTabsHeight();
						return;
					}

					let bottomHeight = 0;
					const bottomRect = await this.getTabsRect('#bottom');
					if (bottomRect) {
						bottomHeight = bottomRect.height;
					}

					thiz.tabsMainHeight = systemInfo.windowHeight - rect.top - bottomHeight - 3 + 'px';
				}, 10);
			},
			// 获取一个目标元素的高度
			getElTabsRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query
						.select('.' + elClass)
						.fields(
							{
								size: true
							},
							(res) => {
								// 如果节点尚未生成，res值为null，循环调用执行
								if (!res) {
									setTimeout(() => {
										this.getElTabsRect(elClass);
									}, 10);
									return;
								}
								this[dataVal] = res.height;
								resolve();
							}
						)
						.exec();
				});
			},
			// 设置左边菜单的滚动状态
			async setTabsStatus(index) {
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.tabsHeight == 0 || this.tabsItemHeight == 0) {
					await this.getElTabsRect('tabs-title', 'tabsHeight');
					await this.getElTabsRect('tabs-item-title', 'tabsItemHeight');
				}
				// 将菜单活动item垂直居中
				this.tabsTop = index * this.tabsItemHeight + this.tabsItemHeight / 2 - this.tabsHeight / 2;
			},
			changeTabs(evt) {
				let { index } = evt.currentTarget.dataset;
				if (index == this.tabsIndex) return;
				this.setData({
					tabsIndex: index,
					tabsMainIndex: index
				});
				this.setTabsStatus(index);
				this.navigateTo({ title_like: this.title, refresh: 1, type: 'listApi' });
			},

			rightScrollTabs(evt) {
				let tabsLoad = this.tabsLoad;
				let that = this;
				let cates = this.cates.rows;
				let tabHeight = 0;
				if (this.tabsTimer) return;
				if (tabsLoad) {
					for (let i = 0; i < cates.length; i++) {
						let view = uni.createSelectorQuery().select('#tabs-content-' + i);
						view.fields(
							{
								size: true
							},
							(data) => {
								if (data) {
									cates[i].top = tabHeight;
									tabHeight = tabHeight + data.height;
									cates[i].bottom = tabHeight;
								}
							}
						).exec();
					}
					that.setData({
						tabsLoad: false,
						'cates.rows': cates
					});
				}
				// 节流
				setTimeout(() => {
					this.tabsTimer = null;
					let scrollTop = evt.detail.scrollTop + 20;
					for (let i = 0; i < cates.length; i++) {
						if (scrollTop > cates[i].top && scrollTop < cates[i].bottom) {
							that.setData({
								tabsIndex: i
							});
							this.setTabsStatus(i);
							return false;
						}
					}
				}, 20);
			}
		},
		onPullDownRefresh() {
			// 列表数据 API请求方法
			this.listNum = 1;
			this.listApi();

			uni.stopPullDownRefresh();
		},
		onReachBottom() {
			// 列表数据 API请求方法
			this.listApi();
		}
	};
</script>

<style lang="scss" scoped>
	.title-clz {
		background-color: #ffffff;
		margin-left: 16rpx;
		border-bottom-left-radius: 12rpx;
		overflow: hidden;
		width: calc(100% - 16rpx - 16rpx) !important;
		border-top-left-radius: 12rpx;
		margin-top: 16rpx;
		border-top-right-radius: 12rpx;
		border-bottom-right-radius: 12rpx;
		margin-bottom: 16rpx;
		margin-right: 16rpx;
	}
	.tabs-clz {
		border-top: 6rpx solid #ffffff;
	}
	.tabs-content {
		height: 100%;
	}
	.tabs-title {
		height: 80vh;
		width: 200rpx;
		background-color: #ffffff !important;
	}
	.flex14-clz {
		background-color: #ffffff;
		padding-top: 20rpx;
		padding-left: 20rpx;
		padding-bottom: 20rpx;
		padding-right: 20rpx;
	}
	.text7-clz {
		font-weight: bold;
		font-size: 28rpx !important;
	}
	.flex2-clz {
		padding-top: 20rpx;
		border-bottom-left-radius: 12rpx;
		padding-left: 20rpx;
		padding-bottom: 20rpx;
		border-top-right-radius: 12rpx;
		margin-right: 20rpx;
		background-color: #ffffff;
		margin-left: 20rpx;
		box-shadow: 0rpx 0rpx 12rpx 2px rgba(219, 219, 219, 0.31);
		overflow: hidden;
		width: calc(100% - 20rpx - 20rpx) !important;
		border-top-left-radius: 12rpx;
		margin-top: 10rpx;
		border-bottom-right-radius: 12rpx;
		margin-bottom: 10rpx;
		padding-right: 20rpx;
	}
	.flex4-clz {
		background-color: rgba(236, 236, 236, 0.37);
		border-bottom-left-radius: 12rpx;
		overflow: hidden;
		border-top-left-radius: 12rpx;
		border-top-right-radius: 12rpx;
		border-bottom-right-radius: 12rpx;
	}
	.image1-size {
		height: 160rpx !important;
		width: 200rpx !important;
	}
	.flex20-clz {
		padding-top: 10rpx;
		flex: 1;
		padding-left: 10rpx;
		padding-bottom: 10rpx;
		padding-right: 10rpx;
	}
	.text11-clz {
		font-weight: bold;
		font-size: 28rpx !important;
	}
	.text8-clz {
		color: #f20000;
		font-weight: bold;
		font-size: 28rpx !important;
	}
	.text9-clz {
		text-decoration: line-through;
	}
	.flex-clz {
		padding-top: 20rpx;
		padding-left: 20rpx;
		padding-bottom: 20rpx;
		padding-right: 20rpx;
	}
	.image-size {
		height: 400rpx !important;
		width: 400rpx !important;
	}
	.text-clz {
		color: #969696;
		font-size: 28rpx !important;
	}
	.flex1-clz {
		border-top: 2rpx solid #e4e4e4;
		padding-top: 16rpx;
		border-bottom-left-radius: 0rpx;
		bottom: 0rpx;
		padding-left: 16rpx;
		padding-bottom: 16rpx;
		border-top-right-radius: 24rpx;
		background-color: #ffffff;
		box-shadow: 0rpx 4rpx 12rpx rgba(31, 31, 31, 0.16);
		overflow: visible;
		left: 0rpx;
		border-top-left-radius: 24rpx;
		border-bottom-right-radius: 0rpx;
		padding-right: 16rpx;
	}
	.flex8-clz {
		flex: 1;
	}
	.image2-size {
		height: 48rpx !important;
		width: 48rpx !important;
	}
	.flex11-clz {
		color: #fa240b;
		flex: 1;
	}
	.image8-size {
		height: 48rpx !important;
		width: 48rpx !important;
	}
	.flex5-clz {
		flex: 1;
	}
	.text2-clz {
		border: 2rpx solid #eee;
		border-bottom-left-radius: 40rpx;
		-webkit-animation-duration: 5000ms;
		color: #ffffff;
		animation-delay: 1000ms;
		-webkit-animation-delay: 1000ms;
		border-top-right-radius: 40rpx;
		right: -8rpx;
		background-color: rgba(255, 17, 17, 0.91);
		animation-duration: 5000ms;
		flex-shrink: 0;
		overflow: hidden;
		top: -8rpx;
		width: 16rpx !important;
		border-top-left-radius: 40rpx;
		border-bottom-right-radius: 40rpx;
		position: absolute;
		height: 16rpx !important;
	}
	.image5-size {
		height: 48rpx !important;
		width: 48rpx !important;
	}
	.flex6-clz {
		flex: 1;
	}
	.image3-size {
		height: 48rpx !important;
		width: 48rpx !important;
	}
	.flex12-clz {
		flex: 1;
	}
	.image4-size {
		height: 48rpx !important;
		width: 48rpx !important;
	}
	.container329916 {
		background-color: #f5f5f5;
	}
</style>
